package com.share.beta.chain;

import com.share.beta.chain.handler.AbstractCallHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Collection;

@Component
public class ChainMain {
    @Autowired
    private ApplicationContext context;

    private Collection<AbstractCallHandler> handlers;

    public void start(String type) {
        if (handlers == null) {/*先初始化赋值*/
            handlers = context.getBeansOfType(AbstractCallHandler.class).values();
        }

        for (AbstractCallHandler handler : handlers) {
            //识别接口类型：普通接口,挡板接口,转发接口
            if (handler.type.equals(type)) {
                handler.call();
            }
        }
    }

    @PostConstruct
    public void init() {
        start("http");
    }
}

